package com.ftg.learn.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ftg.learn.mall.entity.PmsSkuStock;
import com.ftg.learn.mall.mapper.PmsSkuStockMapper;
import com.ftg.learn.mall.service.IPmsSkuStockService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ftg.learn.util.ResEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;

import java.sql.SQLException;
import java.util.List;

/**
 * <p>
 * sku的库存 服务实现类
 * </p>
 *
 * @author mallCommon
 * @since 2022-04-19
 */
@Service
public class PmsSkuStockServiceImpl extends ServiceImpl<PmsSkuStockMapper, PmsSkuStock> implements IPmsSkuStockService {


    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    PmsSkuStockMapper pmsSkuStockMapper;

    /**
     * 批量更新sku库存信息
     * @param skuStockList
     * @return
     */
    @Override
    @Transactional(rollbackFor = SQLException.class)
    public boolean updateSku(List<PmsSkuStock> skuStockList){
        Integer stock = 0;
        for (PmsSkuStock pmsSkuStock : skuStockList){
            QueryWrapper<PmsSkuStock> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("sku_stock_id",pmsSkuStock.getSkuStockId());
            this.update(pmsSkuStock,queryWrapper);
            stock += pmsSkuStock.getStock();
        }
        pmsSkuStockMapper.updateProductStock(stock,skuStockList.get(0).getProductId());
        return true;
    }



}
